﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CUREBusinessLogicLayer;
using System.Text.RegularExpressions;

/*************************************************************************************

     * 作   者： 张 颖
  
*************************************************************************************/
namespace CURE.Handler
{
    /// <summary>
    /// ModulHandler 的摘要说明
    /// </summary>
    public class ModulHandler : IHttpHandler
    {
        ModulBll modelBll = new ModulBll();
        EventLogBll eventLogBll = new EventLogBll();
        public void ProcessRequest(HttpContext context)
        {
            #region 操作参数
            string action = context.Request.Form["action"];
            string modelName = context.Request.Form["modelName"];
            string url = context.Request.Form["url"];
            string fatherName = context.Request.Form["fatherName"]; 
            string editAction_id = context.Request.Form["id"];
            string delUrl = context.Request.QueryString["url"];
            string actionSort = context.Request.Form["actionSort"];
            if (actionSort == null || actionSort.Length == 0)
            {
                actionSort = "0";
            }
            if (action == null || action == "")
            {
                action = context.Request.QueryString["action"];
            }           
            #endregion
            #region  登录用户id
            string masterID = string.Empty;
            if (HttpContext.Current.Request.Cookies["MWS_User"] != null)
            {
                masterID = HttpContext.Current.Request.Cookies["MWS_User"]["ID"];
            }
            #endregion
            #region 验证
            Regex reg = new Regex(@"[\u4e00-\u9fa5]");
            #endregion
            #region 增删改操作
            if (fatherName == "")
            {
                context.Response.Write(failure("请选择上级模块"));
            }
            else
            {
                if (action == "add")
                {
                    int maxid = modelBll.GetMaxId();
                    int id = maxid + 1;
                    bool isExist = modelBll.SelectModelByurl(id.ToString(), url);
                    if (isExist)
                    {
                        context.Response.Write(failure("该模块已存在"));
                    }
                    else
                    {
                        if (!reg.IsMatch(url))
                        {
                            bool addSuccess = modelBll.InsertSuccess(id.ToString(), modelName, url, fatherName, actionSort);
                            if (addSuccess)
                            {
                                context.Response.Write(success("SystemManagement/ModuleManagement.aspx"));
                                IDictionary<string, object> dic = new Dictionary<string, object>();
                                dic.Add("masterid", masterID);
                                dic.Add("msg", "模块管理操作：新增模块，ID号为：" + id + "。");
                                eventLogBll.InsertEventLog(dic);
                            }
                            else
                            {
                                context.Response.Write(failure("添加失败"));
                            }
                        }
                        else
                        {
                            context.Response.Write(failure("url不允许有汉字"));
                        }
                    }
                }
                else if (action == "edit")
                {
                    bool editIsExist = modelBll.SelectModelByurl(editAction_id, url);
                    if (editIsExist)
                    {
                        context.Response.Write(failure("url已存在"));
                    }
                    else
                    {
                        if (!reg.IsMatch(url))
                        {
                            bool editSuccess = modelBll.UpdateScuccess(editAction_id, modelName, url, fatherName, actionSort);
                            if (editSuccess)
                            {
                                context.Response.Write(success("SystemManagement/ModuleManagement.aspx"));
                                IDictionary<string, object> dic = new Dictionary<string, object>();
                                dic.Add("masterid", masterID);
                                dic.Add("msg", "模块管理操作：修改模块，ID号为" + editAction_id + "。");
                                eventLogBll.InsertEventLog(dic);
                            }
                            else
                            {
                                context.Response.Write(failure("修改失败"));
                            }
                        }
                        else
                        {
                            context.Response.Write(failure("url不允许有汉字"));
                        }
                    }
                }
                else if (action == "delete")
                {
                    bool delSuccess = modelBll.DelSuccess(delUrl);
                    if (delSuccess)
                    {
                        context.Response.Write(success("", "SystemManagement/ModuleManagement.aspx"));
                        IDictionary<string, object> dic = new Dictionary<string, object>();
                        dic.Add("masterid", masterID);
                        dic.Add("msg", "模块管理操作：删除模块，ID号为" + delUrl + "。");
                        eventLogBll.InsertEventLog(dic);
                    }
                    else
                    {
                        context.Response.Write(failure("删除失败"));
                    }
                }
                else
                {
                    context.Response.Write(failure("无指定操作"));
                }
            }
            #endregion
        }
        #region 提示信息
        /// <summary>
        /// 操作失败(无参数)
        /// </summary>
        /// <returns></returns>
        public string failure()
        {
            return "{\"statusCode\":\"300\", \"message\":\"操作失败\"}";
        }
        /// <summary>
        /// 操作失败(有参数)
        /// </summary>
        /// <param name="str">错误提示</param>
        /// <returns></returns>
        public string failure(string str)
        {
            return "{\"statusCode\":\"300\", \"message\":\"操作失败!" + str + "\"}";
        }
        /// <summary>
        /// 操作成功(无参数)
        /// </summary>
        /// <returns></returns>
        public string success()
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\",\"callbackType\":\"closeCurrent\"}";
        }

        public string success1()
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\"}";
        }

        /// <summary>
        /// 操作成功(有参数)
        /// </summary>
        /// <param name="str">信息</param>
        /// <param name="navTabId">页面指向</param>
        /// <returns></returns>
        public string success(string str, string navTabId)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!" + str + "\",\"navTabId\":\"" + navTabId + "\"}";
        }
        public string success(string navTabId)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\",\"callbackType\":\"closeCurrent\",\"navTabId\":\"" + navTabId + "\"}";
        }
        #endregion
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}